package com.oig.sys.base.service.impl;

import com.oig.common.exception.BadRequestException;
import com.oig.sys.base.dao.ITbCountryDao;
import com.oig.sys.base.dao.ITbCountryGroupMemberDao;
import com.oig.sys.base.dto.CountryDto;
import com.oig.sys.base.dto.CountryGroupMemberDto;
import com.oig.sys.base.entity.TbCountryGroupMember;
import com.oig.sys.base.service.ITbCountryGroupMemberService;
import com.oig.sys.base.vo.CountryGroupMemberVo;
import com.oig.sys.base.vo.CountryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbCountryGroupMemberServiceImpl implements ITbCountryGroupMemberService {

	private final ITbCountryGroupMemberDao tbCountryGroupMemberDao;
	private final ITbCountryDao countryDao;


	@Autowired
	public TbCountryGroupMemberServiceImpl(ITbCountryGroupMemberDao tbCountryGroupMemberDao,
										   ITbCountryDao countryDao){
		 this.tbCountryGroupMemberDao = tbCountryGroupMemberDao;
		 this.countryDao = countryDao;
	}

	@Transactional
	@Override
	public List<Integer> addGroupMember(List<CountryGroupMemberDto> groupMemberDtoList) {
	    List<Integer> memberIdList = new ArrayList<>();
	    for (CountryGroupMemberDto groupMemberDto : groupMemberDtoList){
            TbCountryGroupMember countryGroupMember = new TbCountryGroupMember();
            countryGroupMember.setGroupId(groupMemberDto.getGroupId());
            countryGroupMember.setCountryCode(groupMemberDto.getCountryCode());
            this.tbCountryGroupMemberDao.save(countryGroupMember) ;
            memberIdList.add(countryGroupMember.getId());
        }
		return memberIdList;
	}

	@Transactional
	@Override
	public void delGroupMember(List<Integer> memberIdList) {
	    for (Integer memberId : memberIdList){
		    this.tbCountryGroupMemberDao.deleteById(memberId);
        }
	}

	@Transactional
	@Override
	public CountryGroupMemberVo addGroupMember(Integer groupId, String countryCode) {
		CountryVo countryVo = this.countryDao.getVoByCode(countryCode);
		if (countryVo==null){
			throw new BadRequestException("国家不存在") ;
		}
		TbCountryGroupMember countryGroupMember = new TbCountryGroupMember();
		countryGroupMember.setGroupId(groupId);
		countryGroupMember.setCountryCode(countryCode);
		this.tbCountryGroupMemberDao.save(countryGroupMember) ;
		CountryGroupMemberVo groupMemberVo = new CountryGroupMemberVo();
		groupMemberVo.setId(countryGroupMember.getId());
		groupMemberVo.setCode(countryVo.getCode());
		groupMemberVo.setApplyCode(countryVo.getApplyCode());
		groupMemberVo.setName(countryVo.getName());
		return groupMemberVo;
	}

	@Transactional
	@Override
	public List<CountryGroupMemberVo> addGroupMembers(Integer groupId, List<CountryDto> countryList) {
		List<CountryGroupMemberVo> countryVoList = new ArrayList<>();
		for (CountryDto countryDto : countryList){
			countryVoList.add(this.addGroupMember(groupId,countryDto.getCode()));
		}
		return countryVoList;
	}

	@Transactional
	@Override
	public List<CountryGroupMemberVo> updateGroupMembers(Integer groupId, List<CountryDto> countryList) {
		if (countryList == null){
			throw new BadRequestException("未选择角色");
		}
		List<CountryGroupMemberVo> memberVoList = new ArrayList<>();
		List<Integer> userGMList = new ArrayList<>() ;
		for (CountryDto countryDto : countryList) {
			if (countryDto.getCode()==null){
				throw new BadRequestException("角色id为空");
			}
			CountryGroupMemberVo gm = this.tbCountryGroupMemberDao.getByCountryCodeAndGroupId(countryDto.getCode(), groupId) ;
			if (gm==null){
				gm = this.addGroupMember(groupId,countryDto.getCode());
			}
			memberVoList.add(gm) ;

			userGMList.add(gm.getId()) ;
		}
		List<TbCountryGroupMember> delList = this.tbCountryGroupMemberDao.findByGroupIdAndIdNotIN(groupId, userGMList);
		if (delList!=null &&!delList.isEmpty()){
			this.tbCountryGroupMemberDao.deleteAll(delList);//可以开启jpa批量操作
		}
		return memberVoList;
	}




	@Override
	public List<CountryGroupMemberVo> findGroupAllMember(Integer groupId) {
		return this.tbCountryGroupMemberDao.getGroupMemberByGroupId(groupId) ;
	}

}
